package org.quickjsf.core.base.controller;

import javax.faces.event.ActionEvent;

import org.primefaces.model.MenuModel;
import org.quickjsf.core.base.controller.support.EditableDTPanelSupport;
import org.quickjsf.core.base.viewmodel.EditableViewModel;
import org.quickjsf.core.base.viewmodel.support.EditableViewModelSupport;

public abstract class EditableDTPanelController<T> extends BaseDTPanelController<T> implements IEditable<T> {
	
	private EditableDTPanelSupport<T> editableDTPanelSupport;
	
	public EditableDTPanelController(EditableViewModel<T> viewModel) throws Exception {
		super(viewModel);
		initEditableDTPanelSupport(viewModel.getEditableViewModel());

	}
	
	private void initEditableDTPanelSupport(EditableViewModelSupport<T> editableViewModel) {
		editableDTPanelSupport = new EditableDTPanelSupport<T>(editableViewModel){
			@Override
			public BaseDTPanelController<T> getBaseDTPanelController() {
				return EditableDTPanelController.this;
			}
		};
	}
	
	@Override
	public void addMenuBarItem(MenuModel menuModel) {
		super.addMenuBarItem(menuModel);
		editableDTPanelSupport.addEditableMenuModel(menuModel);
	}
	
	public void save(ActionEvent actionEvent) throws Exception {
		editableDTPanelSupport.save(this);
	}
	
	public void delete(ActionEvent actionEvent) {
		editableDTPanelSupport.delete(this);
	}
	
	public EditableDTPanelSupport<T> getEditableDTPanelSupport() {
		return editableDTPanelSupport;
	}

	public void setEditableDTPanelSupport(
			EditableDTPanelSupport<T> editableDTPanelSupport) {
		this.editableDTPanelSupport = editableDTPanelSupport;
	}
}
